package com.chc.exer;

/**
 * @Author: chc
 * @Date: 2024/11/10/17:06
 * @Description:
 */
public class TitcketThread {

    public static void main(String[] args) {
        Ticket2 t1 = new Ticket2();
        Ticket2 t2 = new Ticket2();
        Ticket2 t3 = new Ticket2();
        t1.start();
        t2.start();
        t3.start();
    }
}

class Ticket2 extends Thread{
        private int ticket = 100;

        @Override
        public void run() {
            while(true){
                synchronized (Ticket2.class){
                    if (ticket > 0) {
                        System.out.println(Thread.currentThread().getName() + "卖出第" + ticket + "张票");
                        ticket--;
                    }else{
                        break;
                    }
                }
            }
        }
}
